# 设置输出编码为UTF-8，确保支持Unicode字符
$OutputEncoding = [System.Text.Encoding]::UTF8

# 网卡名称
$adapter1 = "Realtek PCIe GbE Family Controller"  # 以太网适配器名称
$adapter2 = "Realtek PCIe GBE Family Controller"   # 无线适配器名称

# 速度阈值，单位为字节/秒
$thresholdSpeed = 100 * 1024  
# 检测间隔时间，单位为秒
$speedCheckDuration = 5  
# 切换适配器的超时时间，单位为秒
$switchTimeout = 200  

# 禁用适配器函数
function Disable-Adapter {
    param([string]$name)
    try {
        Disable-NetAdapter -Name $name -ErrorAction Stop
    } catch {
        Write-Host "Failed to disable adapter ${name}: $_"
    }
}

# 启用适配器函数
function Enable-Adapter {
    param([string]$name)
    try {
        Enable-NetAdapter -Name $name -ErrorAction Stop
    } catch {
        Write-Host "Failed to enable adapter ${name}: $_"
    }
}

# 获取当前适配器名称函数
function Get-CurrentAdapterName {
    param([string]$description)
    try {
        $currentAdapter = Get-NetAdapter | Where-Object { $_.InterfaceDescription -eq $description } | Select-Object -ExpandProperty Name
    } catch {
        Write-Host "Failed to get current adapter name: $_"
        $currentAdapter = $null
    }
    return $currentAdapter
}
# 获取当前适配器速度函数
function Get-CurrentAdapterSpeed {
    param([string]$name)
    try {
        # 获取两个时间点的接收字节数
        $startTime = Get-Date
        $startStats = Get-NetAdapterStatistics -Name $name -ErrorAction Stop
        Start-Sleep -Seconds $speedCheckDuration
        $endStats = Get-NetAdapterStatistics -Name $name -ErrorAction Stop
        $endTime = Get-Date

        # 计算接收速度
        $receivedBytes = $endStats.ReceivedBytes - $startStats.ReceivedBytes
        $elapsedTime = ($endTime - $startTime).TotalSeconds
        $speed = [math]::Round($receivedBytes / $elapsedTime)
    } catch {
        Write-Host "Failed to get speed for adapter ${name}: $_"
        $speed = 0
    }
    return $speed / 1024
}


# 初始化变量
$currentAdapter = $adapter2
$switchCount = 0

# 开始监控和切换
while ($true) {
    try {
        # 获取当前正在使用的适配器名称
        $currentAdapterName = Get-CurrentAdapterName -description $currentAdapter
        if (-not $currentAdapterName) {
            Write-Host "Failed to get current adapter name. Exiting loop."
            break
        }
        Write-Host "Current adapter in use: $currentAdapterName"

        # 获取当前适配器的速度
        $speed = Get-CurrentAdapterSpeed -name $currentAdapterName
        Write-Host "Current speed: $speed kb/s"

        # 检查是否需要切换适配器
        if ($speed -gt $thresholdSpeed) {
            # 速度超过阈值，增加切换计数器
            $switchCount++
            if ($switchCount * $speedCheckDuration -ge $switchTimeout) {
                # 切换超时，执行切换适配器操作
                if ($currentAdapter -eq $adapter1) {
                    Disable-Adapter -Name $adapter1
                    Enable-Adapter -Name $adapter2
                    $currentAdapter = $adapter2
                } else {
                    Disable-Adapter -Name $adapter2
                    Enable-Adapter -Name $adapter1
                    $currentAdapter = $adapter1
                }
                # 重置切换计数器
                $switchCount = 0
            }
        } else {
            # 速度低于阈值，重置切换计数器
            $switchCount = 0
        }
    } catch {
        Write-Host "Error occurred: $_"
    }

    # 等待指定时间后再次检查
    Start-Sleep -Seconds $speedCheckDuration
}
